import { sequelize } from "../../init";
import { DataTypes } from "sequelize";
import { User } from "./User";
import { Authority } from "./Authority";

export const UserPermissions = sequelize.define(
    "UserPermissions",
    {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        userId: {
            type: DataTypes.INTEGER,
            allowNull: false,
        },
        authorityId: {
            type: DataTypes.INTEGER,
            allowNull: false,
        },
    },
    { freezeTableName: true, paranoid: true }
);

// 配权限
User.belongsToMany(Authority, {
    through: {
        model: UserPermissions,
    },
    foreignKey: "userId",
    constraints: false,
});

Authority.belongsToMany(User, {
    through: {
        model: UserPermissions,
    },
    foreignKey: "authorityId",
    constraints: false,
});
